﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Exception = System.Exception;

namespace CADShowTools
{
    public class Commands : IExtensionApplication
    {
        private static int _showType;

        private static readonly CurrentShowForm Frm = new CurrentShowForm(Application.MainWindow.Handle)
        {
            Visible = true
        };

        public void Initialize()
        {
            var dm = Application.DocumentManager;

            if (dm != null)
            {
                dm.DocumentCreated += Dm_DocumentCreated;
                foreach (Document doc in dm)
                {
                    doc.Editor.Rollover += Editor_Rollover;
                }
                dm.MdiActiveDocument.Editor.WriteMessage("提示功能已加载!\n");
                dm.MdiActiveDocument.Editor.WriteMessage(">>>>>V0~v3切换提示模式!<<<<<\n");
            }
            SetShowTypeOne();
            ShowInfo("");
        }

        public void Terminate()
        {
            try
            {
                var dm = Application.DocumentManager;
                if (dm != null)
                {
                    var ed = dm.MdiActiveDocument.Editor;
                    ed.Rollover -= Editor_Rollover;
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }

        private void Editor_Rollover(object sender, RolloverEventArgs e)
        {
            var ed = (Editor) sender;
            var path = e.Highlighted;
            var ids = path.GetObjectIds();
            if (ids.Length == 0)
            {
                ShowInfo("");
                return;
            }


            using (var loc = ed.Document.LockDocument())
            {
                var info = "\nhello\n";
                using (var tr = ed.Document.TransactionManager.StartTransaction())
                {
                    var ent = (Entity) tr.GetObject(ids[0], OpenMode.ForRead);
                    info += $"Layer:{ent.Layer}\r\n";
                    ShowInfo(info);
                    tr.Commit();
                }
            }
        }

        private void Dm_DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            e.Document.Editor.Rollover += Editor_Rollover;
        }

        private void ShowInfo(string info)
        {
            Frm.Showinfo(info, _showType);
        }

        private void UpdateSysVariable(int newtype)
        {
            ShowInfo("");
            _showType = newtype;
            Application.SetSystemVariable("ROLLOVERTIPS", _showType == 0 ? 1 : 0);
        }

        [CommandMethod("v0")]
        public void SetShowTypeZero()
        {
            UpdateSysVariable(0);
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("显示模式已还原");
        }

        [CommandMethod("v1")]
        public void SetShowTypeOne()
        {
            UpdateSysVariable(1);
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("显示模式已设为 【标签】");
        }

        [CommandMethod("v2")]
        public void SetShowTypeTwo()
        {
            UpdateSysVariable(2);
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("显示模式已设为 【HTML】");
        }

        [CommandMethod("v3")]
        public void SetShowTypeThree()
        {
            UpdateSysVariable(3);
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("显示模式已设为 【列表】");
        }
    }
}